package xyz.quanguanjia.masterboy;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import xyz.quanguanjia.masterboy.decoration.HomeItemDecoration;
import xyz.quanguanjia.masterboy.me.MBDEditAvatarActivity;
import xyz.quanguanjia.masterboy.me.MBDEditInfoActivity;
import xyz.quanguanjia.masterboy.model.MBDAccountInfoModel;

public class MBDPersonalInfoActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private BroadcastReceiver loginSuccessReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 接收到登录成功的广播，执行刷新操作
            if (MBDNotificationCenter.NOTIFICATION_UPDATE_USERINFO.equals(intent.getAction())) {
                // 在这里更新头像等信息
                updateUserInfo();
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);

        //导航栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 方法2：禁用标题的显示（推荐）
        getSupportActionBar().setDisplayShowTitleEnabled(false);


        // 注册广播接收器
        IntentFilter filter = new IntentFilter(MBDNotificationCenter.NOTIFICATION_UPDATE_USERINFO);
        LocalBroadcastManager.getInstance(this).registerReceiver(loginSuccessReceiver, filter);

        MBDToolbarUtils.setCenterTitle(toolbar, "个人信息");
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });

        this.recyclerView = findViewById(R.id.recyclerView_personal_info);

        // 设置上拉加载更多监听器
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 在这里判断是否到达列表底部，触发加载更多的操作
            }
        });

        List<PersonalInfoCellItemModel> cellItemModels = cellItemModels();

        LinearLayoutManager layoutManager = new LinearLayoutManager(MBDApplication.getContext());
        recyclerView.setLayoutManager(layoutManager);
        HomeItemDecoration hi = new HomeItemDecoration(new HomeItemDecoration.HomeItemClickListener() {
            @Override
            public void onItemClick(int position) {
                PersonalInfoCellItemModel itemModel = cellItemModels.get(position);
                if (itemModel.type == PersonalInfoAdapter.TYPE_PERSONAL_INFO_AVATAR) {
                    Intent intent = new Intent(MBDPersonalInfoActivity.this, MBDEditAvatarActivity.class);
                    intent.putExtra("imageUrl", itemModel.image);
                    startActivity(intent);
                } else if (itemModel.type == PersonalInfoAdapter.TYPE_PERSONAL_INFO) {
                    Intent intent = new Intent(MBDPersonalInfoActivity.this, MBDEditInfoActivity.class);
                    if (position == 1) {
                        intent.putExtra("title", "设置用户名");
                        intent.putExtra("key", "realName");
                    } else {
                        intent.putExtra("title", "设置微信号");
                        intent.putExtra("key", "weChatNumber");
                    }
                    intent.putExtra("content", itemModel.detail);
                    startActivity(intent);
                }

            }
        });
        recyclerView.addItemDecoration(hi);
        recyclerView.addItemDecoration(new DividerItemDecoration(MBDApplication.getContext(), LinearLayoutManager.VERTICAL));

        PersonalInfoAdapter infoAdapter = new PersonalInfoAdapter(cellItemModels);
        this.recyclerView.setAdapter(infoAdapter);
    }

    public List<PersonalInfoCellItemModel> cellItemModels() {
        List<PersonalInfoCellItemModel> ms = new ArrayList<PersonalInfoCellItemModel>();
        MBDAccountInfoModel infoModel = MBDAccountInfoModel.current();

        PersonalInfoCellItemModel model1 = new PersonalInfoCellItemModel();
        model1.title = "头像";
        model1.image = infoModel.getAvatarUrl();
        model1.type = PersonalInfoAdapter.TYPE_PERSONAL_INFO_AVATAR;
        ms.add(model1);

        PersonalInfoCellItemModel model2 = new PersonalInfoCellItemModel();
        model2.title = "用户名";
        model2.detail = infoModel.getRealName();
        model2.type = PersonalInfoAdapter.TYPE_PERSONAL_INFO;
        ms.add(model2);

        PersonalInfoCellItemModel model3 = new PersonalInfoCellItemModel();
        model3.title = "微信号";
        model3.type = PersonalInfoAdapter.TYPE_PERSONAL_INFO;
        model3.detail = infoModel.getWeChatNumber();
        ms.add(model3);
        return ms;
    }

    public void updateUserInfo() {
        PersonalInfoAdapter infoAdapter = new PersonalInfoAdapter(this.cellItemModels());
        this.recyclerView.setAdapter(infoAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(this.loginSuccessReceiver);

    }
}